#include <ESP8266WiFi.h>  
#include <ESP8266WiFiMulti.h>
#include <ESP8266WebServer.h>

//#include "webserver.h"

//定义一个变量ButtonPin等于D3
#define ButtonPin D3
// 存储引脚状态用变量
bool pinState; 

//定义一个变量ledPin等于板载led的管脚，管脚为 1
const int ledPin =  LED_BUILTIN;

// int ledState = LOW; 
// unsigned long previousMillis = 0;
// const long interval = 1000; 

// 建立ESP8266WiFiMulti对象,对象名称是 'wifimulti'
ESP8266WiFiMulti wifimulti;

//extern ESP8266WebServer webserver(80);
ESP8266WebServer webserver(80);


void handleRoot()
{
    //网页代码不能分行
    // webserver.send(200,"text/html","<form action=\"LED\" method=\"POST\">
    //                                     <input type=\"submit\" value=\"Toggle LED\">
    //                                 </form>");
    /*  //网页分行方法；
        //定义一个字符串，分行向字符串添加代码
        //例
        String htmlcode;
        htmlcode="<form action=\"LED\" method=\"POST\">\n"
        htmlcode+="<input type=\"submit\" value=\"Toggle LED\">"
        htmlcode+="</form>"


    */
    //3-2-3
    String htmlCode = "<!DOCTYPE html> <html>\n";
    htmlCode +="<head><meta http-equiv='refresh' content='1'/>\n";
    htmlCode +="<title>ESP8266 Butoon State</title>\n";
    htmlCode +="<style>html { font-family: Helvetica; display: inline-block; margin: 0px auto; text-align: center;}\n";
    htmlCode +="body{margin-top: 50px;} h1 {color: #444444;margin: 50px auto 30px;} h3 {color: #444444;margin-bottom: 50px;}\n";
    htmlCode +="</style>\n";
    htmlCode +="</head>\n";
    htmlCode +="<body>\n";
    htmlCode +="<h1>ESP8266 BUTTON STATE</h1>\n";
    
    if(pinState)
        {htmlCode +="<p>Button Status: HIGH</p>\n";}
    else
        {htmlCode +="<p>Button Status: LOW</p>\n";}
        
    htmlCode +="</body>\n";
    htmlCode +="</html>\n";
    webserver.send(200,"text/html",htmlCode);
    //3-2-2
    //webserver.send(200, "text/html", "<form action=\"/LED\" method=\"POST\"><input type=\"submit\" value=\"Toggle LED\"></form>");

}

// void handleLED()
// {
//     digitalWrite(ledPin,!digitalRead(ledPin));
//     webserver.sendHeader("location","/");
//     webserver.send(303);
// }

void handleNotFound()
{
    webserver.send(404,"text/plain","404: Not Found");
}

void setup()
{
    Serial.begin(115200);
    //在这里添加你需要的WiFi账号密码
    wifimulti.addAP("LAGRANGE","abcdef..");
    wifimulti.addAP("OpenWRT-2.4G","skabcdef");
    wifimulti.addAP("MEIZU 16s Pro","skabcdef");
    wifimulti.addAP("Feng","1234567890");
    
    int i=0;
    Serial.println("connecting······");
    while(wifimulti.run()!=WL_CONNECTED)
    {
        delay(1000);
        Serial.print(i);  Serial.print(' ');
        i++;
        if (i>20)
            break;
    }
    if (WiFi.status()!=WL_CONNECTED)
        Serial.println("connect timeout");
    else
    {
        Serial.print("WiFi Connect");
        Serial.print("IP address:"); 
        Serial.println(WiFi.localIP());
    }
    pinMode(ledPin,OUTPUT);
    pinMode(ButtonPin,INPUT_PULLUP);



    webserver.begin();
    //3-2-3
    webserver.on("/",handleRoot);
    webserver.onNotFound(handleNotFound);
    //3-2-2
    // webserver.on("/",HTTP_GET,handleRoot);
    // webserver.on("/LED",HTTP_POST,handleLED);
    // webserver.onNotFound(handleNotFound);
    Serial.println("HTTP esp8266_server started");

}

void loop()
{
    webserver.handleClient();
    //板载led闪烁
    // digitalWrite(ledPin,LOW);
    // delay(1000);
    // digitalWrite(ledPin,HIGH);
    // delay(1000);
    pinState=digitalRead(ButtonPin);
    digitalWrite(ledPin,pinState);
}


